﻿using System;
using System.Data;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Text.RegularExpressions;
using OperGui.ViewModel;
using VI.Fsc.OperatorGui.Models.Fsc.Configuration;
using System.Windows.Resources;
using System.IO;
using System.Xml;
using System.Xml.Linq;
using VI.Fsc.OperatorGui.Models.Application.Util;

namespace OperGui.Views
{
    /// <summary>
    /// Interaction logic for ElementView.xaml
    /// </summary>
    public partial class ElementView : UserControl
    {
        Regex regex;
        string solution;
        string situation;



        public ElementView(string Id)
        {

            InitializeComponent();
            this.DataContext = new ElementViewModel(Id);
            ItemsCollection();
        }

        private void ItemsCollection()
        {
            //List<Elements> listElements = new List<Elements>();
            //listElements.Add(new Elements("IFZ-MT001", "12:39:18", "Warning", "50"));
            //DataList.ItemsSource = listElements;
            //DiagnosticData.ItemsSource = listElements;
        }

        //class Elements
        //{
        //    public string ErrorID { get; set; }
        //    public string Time { get; set; }
        //    public string EEvent { get; set; }
        //    public string Status { get; set; }
        //    public Elements(string _ErrorID, string _Time, string _EEvent, string _Status)
        //    {
        //        this.ErrorID = _ErrorID;
        //        this.Time = _Time;
        //        this.EEvent = _EEvent;
        //        this.Status = _Status;
        //    }
        //}

        private void textboxsearch_TextChanged(object sender, TextChangedEventArgs e)
        {
            {
                FindListViewItem(DataList);
            }
        }

        public void FindListViewItem(DependencyObject obj)
        {
            for (int i = 0; i < VisualTreeHelper.GetChildrenCount(obj); i++)
            {
                ListViewItem lv = obj as ListViewItem;
                if (lv != null)
                {
                    HighlightText(lv);
                }
                FindListViewItem(VisualTreeHelper.GetChild(obj as DependencyObject, i));
            }
        }

        private void HighlightText(Object itx)
        {
            if (itx != null)
            {
                if (itx is TextBlock)
                {
                    regex = new Regex("(" + textboxsearch.Text + ")", RegexOptions.IgnoreCase);
                    TextBlock tb = itx as TextBlock;
                    if (textboxsearch.Text.Length == 0)
                    {
                        string str = tb.Text;
                        tb.Inlines.Clear();
                        tb.Inlines.Add(str);
                        return;
                    }
                    string[] substrings = regex.Split(tb.Text);
                    tb.Inlines.Clear();
                    foreach (var item in substrings)
                    {
                        if (regex.Match(item).Success)
                        {
                            Run runx = new Run(item);
                            runx.Background = Brushes.Red;
                            tb.Inlines.Add(runx);
                        }
                        else
                        {
                            tb.Inlines.Add(item);
                        }
                    }
                    return;
                }
                else
                {
                    for (int i = 0; i < VisualTreeHelper.GetChildrenCount(itx as DependencyObject); i++)
                    {
                        HighlightText(VisualTreeHelper.GetChild(itx as DependencyObject, i));
                    }
                }
            }
        }



        static Stream GetResourceStream(string resourceFile)
        {
            Uri uri = new Uri(resourceFile, UriKind.RelativeOrAbsolute);
            StreamResourceInfo info = Application.GetResourceStream(uri);
            if (info == null || info.Stream == null)
                throw new ApplicationException("Missing resource file: " + resourceFile);
            return info.Stream;
        }

       

        private void DataList_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            string str1 = "Situation:";
            string str2 = "Solution:";
            CStructureItem a = (CStructureItem)DataList.SelectedItem;
            string ErrorID = a.ProblemKey;


            using (Stream stream = GetResourceStream("Data/EventDefinitions.xml"))
            using (XmlReader xmlRdr = new XmlTextReader(stream))
            {

                XDocument doc = XDocument.Load(xmlRdr);

                var item = doc.Descendants("EventDefinition");


                foreach (var Item in item)
                {

                    if (Item.Attribute("ID").Value.ToString() == ErrorID)
                    {
                        situation = Item.Attribute("Name").Value.ToString();
                    }
                }
            }

            solution = CFscUtility.GetFullProblemKey("EventDefinition." + situation);
            SolutionList.Content = str1 + "\r\n" + situation + "\r\n" + "\r\n" + str2 + "\r\n" + solution + "\r\n";


        }
    }
}
